function showDiv(id) {
    $(id).style.visibility = "visible";
}

function closeDiv(id) {
    $(id).style.visibility = "hidden";
}

window.onload = function () {
    showUser();
}

function showUser() {
    axios.get("/user/findAll").then(resp => {
        let info = resp.data;
        let str = "";
        info.forEach(n => {
            str += `<tr><td>${n.name}</td><td>${n.pwd}</td><td>${n.realName}</td><td>${n.level}</td>
<td><input type="button" value=" 删除用户 " onclick="delUser(${n.id})"> 
<input type="button" value=" 修改用户等级 " onclick="showUpdate(${n.id})"></td></tr>`;
        });
        $("data").innerHTML = str;
    });
}

function delUser(id) {

    axios.get("/user/del", {
        params: {
            id
        }
    }).then(resp => {
        let info = resp.data;
        if (info == "ok") {
            showDiv('showInfoDiv');
            $("showInfo").style.color = "black";
            $("showInfo").innerHTML = "删除成功";
            showUser();
        } else {
            showDiv('showInfoDiv');
            $("showInfo").style.color = "red";
            $("showInfo").innerHTML = "你不能删除自己。";
        }

    })
}

function showUpdate(id) {
    $("updateUserId").value =id;
    showDiv('UpdateUserDiv');
    axios.get("/user/findById", {
        params:{
            id
        }
    }).then(resp => {
        let info = resp.data;
        $("nameSpan").innerText = info.name;
        $("realNameSpan").innerText = info.realName;

    })
}

function UpdateUser() {

    axios.get("/user/updateLevel", {
        params: {
            id: $("updateUserId").value,
            level: $("upLevelSelect").value
        }
    }).then(resp=>{
        let  info=resp.data;
        if (info=="ok"){
            showDiv('showInfoDiv');
            $("showInfo").style.color = "black";
            $("showInfo").innerHTML = "修改成功";
            showUser();
        } else {
            showDiv('showInfoDiv');
            $("showInfo").style.color = "red";
            $("showInfo").innerHTML = "修改失败。";
        }
    });
}
function addUser(){
    axios.get("/user/add",{
        params:{
            name:$("userNameText").value,
            realName:$("realNameText").value,
            level:$("levelSelect").value
        }
    }).then(resp=>{
         let info=resp.data;
         if (info=="ok"){
             showDiv('showInfoDiv');
             $("showInfo").style.color = "black";
             $("showInfo").innerHTML = "添加成功";
             showUser();
         } else {
             showDiv('showInfoDiv');
             $("showInfo").style.color = "red";
             $("showInfo").innerHTML = "提交失败,用户名已存在。";
         }
    });
}